// 收集formid
const FORMID_LIST = [];
wx.define('pushFormid', function(formid) {
    // if (wx.isDev) return;
    if (FORMID_LIST.length > 100) {
        FORMID_LIST.shift();
    }
    FORMID_LIST.push(formid);
});
wx.define('pullFormid', function(num = 1) {
    return FORMID_LIST.splice(0, num);
});

module.exports = {
    // 推送formid
    __pushFormid__: function(e) {
        if (!e.detail.formId) return;
        wx.pushFormid(e.detail.formId);
    },

    // 设置data值
    setValue: function(e) {
        if (e.detail && e.detail.formId) {
            wx.pushFormid(e.detail.formId);
        }
        this.setData(e.target.dataset);
    },

    //设置data属性
    setDataTap: function(e) {
        // JSON.parse() 将字符串数据转换成对象
        const data = JSON.parse(e.currentTarget.dataset.data || "{}"),
            originalData = this.data;

        const result = {};
        for (const key in data) {
            if (typeof originalData[key] !== "object") {
                originalData[key] = data[key];
            } else {
                if (typeof originalData[key] === "object" && typeof data[key] === "object") {
                    Object.deepAssign(originalData[key], data[key]);
                } else {
                    originalData[key] = data[key];
                }
            }

            result[key] = originalData[key];
        }

        this.setData(result);
    },

    // 跳转页面
    linkToTap: function(e) {
        const url = e.currentTarget.dataset.url,
            type = e.currentTarget.dataset.navigateType || "navigateTo";
        const isDisabled = e.currentTarget.dataset.disabled;
        if(isDisabled)return;
        // console.log("url:", url, isDisabled)
        wx[type]({
            url: url,
        })
    }
};